package cn.wolfcode.p2p.base.service.impl;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import cn.wolfcode.p2p.base.domain.IpLog;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.mapper.RealAuthMapper;
import cn.wolfcode.p2p.base.mapper.UserInfoMapper;
import cn.wolfcode.p2p.base.query.RealAuthQuery;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.hosting.service.IHostingService;
import cn.wolfcode.p2p.util.AssertUtil;
import cn.wolfcode.p2p.util.BitStatesUtils;
import cn.wolfcode.p2p.util.PageResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class RealAuthServiceImpl implements IRealAuthService {
        @Autowired
        private RealAuthMapper realAuthMapper;
        @Autowired
        private IUserInfoService userInfoService;
        @Autowired
        private IHostingService hostingService;
        @Override
        public void apply(RealAuth realAuth) {
                LoginInfo loginInfo = UserContext.getLoginInfo();
                UserInfo userInfo = userInfoService.getById(loginInfo.getId());
                AssertUtil.instance().isFalse(userInfo.hasRealAuth(),"已经通过实名认证")
                        .isNull(userInfo.getRealAuthId(),"已经提交了认证,请耐心等候!");
                hostingService.bindReal(realAuth,loginInfo);

                RealAuth ra = new RealAuth();
                ra.setRealname(realAuth.getRealname());
                ra.setSex(realAuth.getSex());
                ra.setBornDate(realAuth.getBornDate());
                ra.setIdNumber(realAuth.getIdNumber());
                ra.setAddress(realAuth.getAddress());
                ra.setImage1(realAuth.getImage1());
                ra.setImage2(realAuth.getImage2());
                ra.setApplyTime(new Date());
                ra.setApplier(loginInfo);
                ra.setState(RealAuth.STATE_SUCCESS);
                realAuthMapper.insert(ra);

                //修改USERinfo里的实名认证
                userInfo.setRealAuthId(ra.getId());
                userInfo.addBitState(BitStatesUtils.OP_REAL_AUTH);
                userInfoService.update(userInfo);
        }

        @Override
        public RealAuth getById(Long id) {
                return realAuthMapper.selectByPrimaryKey(id);
        }

        @Override
        public PageResult query(RealAuthQuery qo) {
                //先查出COunt,如果COUNT为0,返回空集合
                int row = realAuthMapper.selectCount(qo);
                if(row==0){
                        return new PageResult(Collections.EMPTY_LIST,0,1,qo.getPageSize());
                }
                List<RealAuth> list =realAuthMapper.query(qo);

                return new PageResult(list,row,qo.getCurrentPage(),qo.getPageSize());
        }

        @Override
        public void audit(Long id, String remark, Integer state) {
                //更新审核对象
                //1判断基础参数
                AssertUtil.instance().isNotNull(id,"出错啦!请联系客服!")
                        .isNotNull(state,"出错啦,请联系客服");
                //2,判断是否审核,避免高并发引发的问题
                RealAuth realAuth = realAuthMapper.selectByPrimaryKey(id);
                //如果不是待审核,就报错
                AssertUtil.instance().isFalse(realAuth.getState()!=RealAuth.STATE_NORMAL,"状态异常!请联系客服");
                //3,设置公共的审核参数
                realAuth.setState(state);
                realAuth.setRemark(remark);
                realAuth.setAuditTime(new Date());//审核时间
                realAuth.setAuditor(UserContext.getLoginInfo());//设置审核人
                update(realAuth); //更新审核对象
                //4,失败或者成功分别的处理
                UserInfo userInfo = userInfoService.getById(realAuth.getApplier().getId());
                if(state==RealAuth.STATE_SUCCESS){

                        userInfo.addBitState(BitStatesUtils.OP_REAL_AUTH);
                        userInfo.setRealName(realAuth.getRealname());
                        userInfo.setIdNumber(realAuth.getIdNumber());
                }else{
                        userInfo.setRealAuthId(null);//删掉认证ID
                }
                //成功,设置用户的状态状态,
                //失败,删除用户的审核对象ID,
                //更新到审核对象
                userInfoService.update(userInfo);
                //


        }

        private void update(RealAuth realAuth) {
                realAuthMapper.updateByPrimaryKey(realAuth);
        }
}
